// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-12-23 14:50:18
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Element\CHEXAParser.cpp
 */

#include "Import/BDFImport/BulkData/Element/CHEXAParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (CHEXA);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/CHEXA/CHEXA.md>
*/
// 以下需要修改
enum CHEXAIndex : std::uint8_t
{
    CHEXAIndex_EID = 1,
    CHEXAIndex_PID = 2,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseCHEXA (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::CHEXA20Data tempData;  // 创建临时的CHEXA数据对象

    // TODO(bug 001) m_dominId的正确填写
    tempData.m_domainId = 1;  // 暂时将m_domainId设置为1，待修复

    // 解析固定数据
    tempData.m_id  = convertToI64 (dividedEntryInfo[CHEXAIndex_EID]);  // 解析并存储EID
    tempData.m_pId = convertToI64 (dividedEntryInfo[CHEXAIndex_PID]);  // 解析并存储PID

    // 用于存储网格点的索引
    int m_gIndex = 0;

    // 计算词条行数
    size_t const rowNum = dividedEntryInfo.size () / 10;  // 每行最多包含10个词条

    // 设置续行符下标
    size_t const endColumnIndex = 9;

    // 节点的第一行从index=3开始（跳过EID、PID、G1等）
    size_t beginColumnIndex = 3;

    // 遍历所有行
    for (size_t curRow = 0; curRow < rowNum; curRow++)
    {
        if (curRow != 0)
        {
            // 除了第一行以外，其他行的第一个字段为节点数据
            beginColumnIndex = 1;
        }

        // 遍历当前行的所有字段
        for (size_t fieldsColumn = beginColumnIndex; fieldsColumn < endColumnIndex; fieldsColumn++)
        {
            // 计算当前字段的索引
            const size_t curIndex = curRow * 10 + fieldsColumn;

            // 获取当前词条的字符串
            std::string& curStrKey = dividedEntryInfo[curIndex];

            // 检查当前字段是否为空白
            bool const isEmpty = is_all_space (curStrKey);

            // 如果当前字段为空白，跳过处理
            if (isEmpty)
            {
                continue;
            }

            // 解析并存储网格点编号
            tempData.m_g[m_gIndex++] = convertToI64 (curStrKey);
        }
    }

    if (m_gIndex == 20)
    {
        return dbServiceFactory->get<IElementService> ()->Add (tempData);
    }

    return dbServiceFactory->get<IElementService> ()->Add (*reinterpret_cast<SG::DataStructure::FEM::CHEXA8Data*> (&tempData));
}